package edu.mama.college.controller.admin;

import edu.mama.college.model.Carousel;
import edu.mama.college.model.JsonResult;
import edu.mama.college.model.Page;
import edu.mama.college.model.Resource;
import edu.mama.college.service.CarouselService;
import edu.mama.college.service.ResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Part;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value="/admin" )
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private CarouselService carouselService;

    /**
     * 素材中心分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/resource/{type}/list")
    public JsonResult getVideoList(@RequestParam("page") int page,
                                   @RequestParam("size") int size,
                                   @PathVariable("type") String type
                                   ){
        Page<Resource> videoList = resourceService.getResourceList(page,size,type);
        return JsonResult.success(videoList);
    }



    /**
     * 图片/视频列表
     * @return
     */
    @GetMapping("/resource/{type}")
    public JsonResult getImageList(@PathVariable("type") String type){
        List<Resource> imageList = resourceService.getResourceList(type);
        return JsonResult.success(imageList);
    }



    /**
     * 上传视频
     * @param part
     * @param duration
     * @return
     */
    @PostMapping("/resource/video/upload")
    public JsonResult uploadVideo(@RequestParam("file") Part part,
                                  @RequestParam("duration") Number duration
                                  ){
        try {
            resourceService.uploadResource(part,duration,"video");
            return JsonResult.success();
        }catch(Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }


    /**
     * 上传图片
     * @param part
     * @return
     */
    @PostMapping("/resource/image/upload")
    public JsonResult uploadImage(@RequestParam("file") Part part){
        try {
            resourceService.uploadResource(part,0,"image");
            return JsonResult.success();
        }catch(Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    /**
     * 删除资源
     */
    @DeleteMapping("/resource/{id}")
    public JsonResult deleteResource(@PathVariable("id") int id){
        try {
            resourceService.deleteResource(id);
            return JsonResult.success();
        }catch(Exception e){
            System.out.println(e.getMessage());
            return JsonResult.fail(e.getMessage());
        }
    }

    /**
     * 调整轮播图
     * @return
     */
    @PostMapping("/resource/carousel")
    public JsonResult updateCarousel(@RequestBody List<Map<String,String>> dataList){

        try{
            carouselService.updateCarousels(dataList);
            return JsonResult.success();
        }catch(Exception e){
            return JsonResult.fail(e.getMessage());
        }

    }





}
